import Vue from 'vue'
import CryptoJS from 'crypto-js/crypto-js'
import md5 from 'js-md5'; /*MD5加密*/
/**
 * AES加密 ：字符串 key iv  返回base64
 */

 export function Encrypt(word){
 	      var keymd5 = md5("huppyjuhuishop").slice(0, 16);
 	      var ivmd5 = md5("huppynet").slice(0, 16);
 	      console.log(keymd5)
	      var key = CryptoJS.enc.Utf8.parse(keymd5);
		  var iv = CryptoJS.enc.Utf8.parse(ivmd5);
	      let srcs = CryptoJS.enc.Utf8.parse(word);
	      var encrypted = CryptoJS.AES.encrypt(srcs, key, {
	        iv: iv,
	        mode: CryptoJS.mode.CBC,
	//      padding: CryptoJS.pad.Pkcs7
	    });
//  return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
    return encrypted.toString()
 }

/**
 * 对象排序
 */
 export function objKeySort(msg,val){
 	      var timestamp = Date.parse(new Date()) / 1000;//时间戳
 	      var dynamic = msg;
 	      var userid = "1";
 	      let objData = {"x-date":timestamp,"x-user-id": userid,dynamic:val};
 	      console.log(objData)    
 	      var newkey = Object.keys(objData).sort();//获取key并进行排序
	      //先用Object内置类的keys方法获取要排序对象的属性名，再利用Array原型上的sort方法对获取的属性名进行排序，newkey是一个数组
		  var newObj = {};//创建一个新的对象，用于存放排好序的键值对
			for (var i = 0; i < newkey.length; i++) {//遍历newkey数组
			    newObj[newkey[i]] = objData[newkey[i]];//向新创建的对象中按照排好的顺序依次增加键值对
			}
		  var arr="";
			for(var i in newObj) {				
				arr += i+"="+newObj[i]+'|'
			}
	    return arr.substring(0, arr.length - 1);//返回排好序的新对象
 }
 
